/*
 * Tint Browser for Android
 * 
 * Copyright (C) 2012 - to infinity and beyond J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.cnbrowser.android.ui.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.cnbrowser.android.R;
import com.cnbrowser.android.model.BookmarkHistoryItem;
import com.cnbrowser.android.model.BookmarksAdapter;
import com.cnbrowser.android.providers.BookmarksProvider;
import com.cnbrowser.android.providers.BookmarksWrapper;
import com.cnbrowser.android.ui.activities.TintBrowserActivity;
import com.cnbrowser.android.ui.cbrowser.StartPageItemClickedListener;
import com.cnbrowser.android.ui.managers.UIManager;
import com.cnbrowser.android.utils.Constants;

public abstract class StartPageFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {



    private View mParentView = null;

    private GridView mGrid;
    private BookmarksAdapter mAdapter;

    private StartPageItemClickedListener mListener = null;

    private OnSharedPreferenceChangeListener mPreferenceChangeListener;

    protected UIManager mUIManager;

    private boolean mInitialized;

    private boolean mListShown = true;

    public StartPageFragment() {
        mInitialized = false;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (!mInitialized) {
            try {
                mUIManager = ((TintBrowserActivity) activity).getUIManager();
            } catch (ClassCastException e) {
                Log.e("StartPageFragment.onAttach()", e.getMessage());
            }

            mInitialized = true;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mParentView == null) {
            mParentView = inflater.inflate(getStartPageFragmentLayout(), container, false);
            mGrid = (GridView) mParentView.findViewById(R.id.StartPageFragmentGrid);
            String[] from = new String[]{BookmarksProvider.Columns.TITLE, BookmarksProvider.Columns.URL};
            int[] to = new int[]{R.id.StartPageRowTitle, R.id.StartPageRowUrl};
            mAdapter = new BookmarksAdapter(
                    getActivity(),
                    R.layout.start_page_row,
                    null,
                    from,
                    to,
                    0,
                    R.drawable.browser_thumbnail);

            if (mGrid != null) {

                mGrid.setAdapter(mAdapter);

                mGrid.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                        if (mListener != null) {
                            BookmarkHistoryItem item = BookmarksWrapper.getBookmarkById(getActivity().getContentResolver(), id);

                            if (item != null) {
                                mListener.onItemClicked(item.getUrl());
                            }
                        }
                    }
                });

                mGrid.setOnTouchListener(mUIManager);
            }


            mPreferenceChangeListener = new OnSharedPreferenceChangeListener() {
                @Override
                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                    if (Constants.PREFERENCE_START_PAGE_LIMIT.equals(key)) {
                        getLoaderManager().restartLoader(0, null, StartPageFragment.this);
                    }
                }
            };

            PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(mPreferenceChangeListener);
        }

        return mParentView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setListShown(false);

        mParentView.postDelayed(new Runnable() {

            @Override
            public void run() {
                if (isAdded()) {
                    getLoaderManager().initLoader(0, null, StartPageFragment.this);
                }
            }
        }, 100);
    }

    @Override
    public void onDestroy() {
        PreferenceManager.getDefaultSharedPreferences(getActivity()).unregisterOnSharedPreferenceChangeListener(mPreferenceChangeListener);
        super.onDestroy();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        int limit;
        try {
            limit = Integer.parseInt(
                    PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(
                            Constants.PREFERENCE_START_PAGE_LIMIT,
                            Integer.toString(getResources().getInteger(R.integer.default_start_page_items_number))));
        } catch (Exception e) {
            limit = getResources().getInteger(R.integer.default_start_page_items_number);
        }

        return BookmarksWrapper.getCursorLoaderForStartPage(getActivity(), limit);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
        setListShown(true);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    public void setOnStartPageItemClickedListener(StartPageItemClickedListener listener) {
        mListener = listener;
    }

    protected abstract int getStartPageFragmentLayout();

    private void setListShown(boolean shown) {

        if (mListShown == shown) {
            return;
        }

        mListShown = shown;

        if (mGrid != null) {
            if (shown) {
                mGrid.setVisibility(View.VISIBLE);
            } else {
                mGrid.setVisibility(View.GONE);
            }
        }
    }

}
